package logic

import (
	"context"
	"day15/model/mysql"

	"day15/houserpc/internal/svc"
	"day15/houserpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseListLogic {
	return &HouseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseListLogic) HouseList(in *pb.HouseListReq) (*pb.HouseListResp, error) {
	// todo: add your logic here and delete this line
	page := in.Page
	pageSize := in.PageSize
	houseModel := mysql.House{}
	house, total, err := houseModel.Paginate(int(page), int(pageSize))
	if err != nil {
		logx.Error("查询失败")
		return nil, err
	}
	var houseInfoList []*pb.HouseInfo
	for _, v := range house {
		houseInfo := &pb.HouseInfo{
			Id:         int64(v.ID),
			HouseName:  v.HouseName,
			HousePrice: float32(v.HousePrice),
			HouseType:  v.HouseType,
			HouseDesc:  v.HouseDesc,
			HouseImg:   v.HouseImg,
		}
		houseInfoList = append(houseInfoList, houseInfo)
	}

	return &pb.HouseListResp{
		HouseList: houseInfoList,
		Total:     total,
	}, nil
}
